package com.chixing.ih.module.task.controller;


import com.chixing.ih.json.ResponseData;
import com.chixing.ih.module.programmer.pojo.dto.ProgrammerEntiretyDTO;
import com.chixing.ih.module.system.pojo.ToEmail;
import com.chixing.ih.module.task.pojo.Contract;
import com.chixing.ih.module.task.pojo.DTO.ContractDTO;
import com.chixing.ih.module.task.pojo.DTO.DeliveryDTO;
import com.chixing.ih.module.task.pojo.DTO.TaskShowDTO;
import com.chixing.ih.module.task.pojo.Task;
import com.chixing.ih.module.task.service.ITaskService;
import com.chixing.ih.util.MailUtil;
import com.chixing.ih.util.PageRequest;
import com.chixing.ih.util.RequestIdUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 目的:
 *
 * @author ：zcw
 * @version: 1.0  2020/9/29 19:13
 */
@RestController
@RequestMapping("task")
public class TaskController {

    @Autowired
    private ITaskService ITaskService;

    @Autowired
    private MailUtil mailUtil;

    @ApiOperation(value = "添加任务接口", notes = "添加任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "isSuerId", value = "发布者id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "mandateTitle", value = "任务标题", required = true, dataType = "String"),
            @ApiImplicitParam(name = "talentCategory", value = "人才类别", required = true, dataType = "String"),
            @ApiImplicitParam(name = "requirementsDescription", value = "需求描述", required = true, dataType = "String"),
            @ApiImplicitParam(name = "skillsRequirements", value = "技能要求", required = true, dataType = "String"),
            @ApiImplicitParam(name = "operationMode", value = "工作方式", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "salary", value = "薪资", required = true, dataType = "Double"),
            @ApiImplicitParam(name = "workCycle", value = "工作周期", required = true, dataType = "String"),
            @ApiImplicitParam(name = "workPlace", value = "工作地点", required = true, dataType = "String"),
            @ApiImplicitParam(name = "isSuerName", value = "发布人姓名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "isSuerTelephone", value = "发布人手机号", required = true, dataType = "String")
    })
    @ApiResponses({
            @ApiResponse(code = 201, message = "添加成功"),
            @ApiResponse(code = 400, message = "添加失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @PostMapping("task")
    public Map<String, Object> insertTask(@RequestBody Task task) {

        int count = ITaskService.insertOne(task);
        if (count == 1) {
            return ResponseData.REP_CREATED_SUCCESS();
        }
        return ResponseData.REP_INVALID_ERROR();
    }

    @ApiOperation(value = "删除一个任务接口", notes = "删除一个任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务id", required = true, dataType = "Long"),
    })
    @ApiResponses({
            @ApiResponse(code = 207, message = "删除成功"),
            @ApiResponse(code = 400, message = "删除失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @DeleteMapping("task/{taskId}")
    public Map<String, Object> deleteOne(@PathVariable("taskId") Long taskId) {

        int count = ITaskService.deleteOne(taskId);
        if (count == 1) {
            return ResponseData.REP_DELETE_SUCCESS();
        }
        return ResponseData.REP_INVALID_ERROR();
    }

    @ApiOperation(value = "删除所有任务接口", notes = "删除所有任务")
    @ApiResponses({
            @ApiResponse(code = 207, message = "删除成功"),
            @ApiResponse(code = 400, message = "删除失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @DeleteMapping("tasks")
    public Map<String, Object> deleteAll(HttpServletRequest request) {

        int count = ITaskService.deleteAll(RequestIdUtil.getMyId(request));
        if (count == 1) {
            return ResponseData.REP_DELETE_SUCCESS();
        }
        return ResponseData.REP_INVALID_ERROR();
    }

    @ApiOperation(value = "更新任务接口", notes = "更新任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "isSuerId", value = "发布者id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "mandateTitle", value = "任务标题", required = true, dataType = "String"),
            @ApiImplicitParam(name = "talentCategory", value = "人才类别", required = true, dataType = "String"),
            @ApiImplicitParam(name = "requirementsDescription", value = "需求描述", required = true, dataType = "String"),
            @ApiImplicitParam(name = "skillsRequirements", value = "技能要求", required = true, dataType = "String"),
            @ApiImplicitParam(name = "operationMode", value = "工作方式", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "salary", value = "薪资", required = true, dataType = "Double"),
            @ApiImplicitParam(name = "workCycle", value = "工作周期", required = true, dataType = "String"),
            @ApiImplicitParam(name = "workPlace", value = "工作地点", required = true, dataType = "String"),
            @ApiImplicitParam(name = "isSuerName", value = "发布人姓名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "isSuerTelephone", value = "发布人手机号", required = true, dataType = "String")
    })
    @ApiResponses({
            @ApiResponse(code = 204, message = "更新成功"),
            @ApiResponse(code = 400, message = "更新失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @PutMapping("task")
    public Map<String, Object> updateOne(@RequestBody Task task) {

        int count = ITaskService.updateOne(task);
        if (count == 1) {
            return ResponseData.REP_ACCEPTED_NO_HANDLE();
        }
        return ResponseData.REP_INVALID_ERROR();
    }

    @ApiOperation(value = "查询一个任务接口", notes = "查询一个任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "任务id", required = true, dataType = "Long"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @GetMapping("task/{taskId}")
    public Map<String, Object> findOne(@PathVariable("taskId") Long taskId) {
        return ResponseData.REP_GET_SUCCESS(ITaskService.findTaskOne(taskId));
    }

    @ApiOperation(value = "查询一个发布任务接口", notes = "查询一个发布任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "任务id", required = true, dataType = "Long"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败")

    })
    @GetMapping("{taskId}")
    public Map<String, Object> getOneTask(@PathVariable("taskId") Long taskId) {
        return ResponseData.REP_GET_SUCCESS(ITaskService.getOneTask(taskId));
    }



    @ApiOperation(value = "查询全部任务接口", notes = "查询全部任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "talentCategories", value = "角色", required = true, dataType = "String"),
            @ApiImplicitParam(name = "workPlace", value = "工作地点", required = true, dataType = "String"),
            @ApiImplicitParam(name = "workWay", value = "工作方式", required = true, dataType = "String"),
            @ApiImplicitParam(name = "list", value = "排序条件", required = true, dataType = "List<Long>")

    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @GetMapping("tasks")
    @ResponseBody
    public Map<String, Object> findAllTask(@ApiIgnore @RequestParam("talentCategories") String talentCategories,
                                           HttpServletRequest request,
                                           @RequestParam("workPlace") String workPlace,
                                           @RequestParam("workWay") Long workWay,
                                           @RequestParam("list") Integer sort, PageRequest pageRequest) {
        List<TaskShowDTO> tasks = ITaskService.findAll(pageRequest, request, talentCategories, workPlace, workWay,
                sort);
        PageInfo<TaskShowDTO> info = new PageInfo<>(tasks);
        return ResponseData.REP_GET_SUCCESS(info);
    }

    @ApiOperation(value = "接受任务但还在审核中的任务接口", notes = "接受任务但还在审核中的任务")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @GetMapping("accepting")
    public Map<String, Object> findAccepting(HttpServletRequest request, PageRequest pageRequest) {
        List<TaskShowDTO> list = ITaskService.findAccepting(pageRequest, RequestIdUtil.getMyId(request));
        PageInfo<TaskShowDTO> info = new PageInfo<>(list);
        return ResponseData.REP_GET_SUCCESS(info);
    }

    @ApiOperation(value = "发布任务但在审核中任务接口", notes = "发布任务但在审核中任务")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @GetMapping("underReviewTask")
    public Map<String, Object> findBySendTaskTypeStatus(HttpServletRequest request, PageRequest pageRequest) {
        List<TaskShowDTO> list = ITaskService.findBySendTaskTypeStatus(pageRequest,
                RequestIdUtil.getMyId(request));
        PageInfo<TaskShowDTO> info = new PageInfo<>(list);
        return ResponseData.REP_GET_SUCCESS(info);
    }

    @ApiOperation(value = "发布任务未被接取任务接口", notes = "发布任务未被接取任务")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @GetMapping("notAcceptedTask")
    public Map<String, Object> findBySendTaskStatus(HttpServletRequest request, @ApiIgnore PageRequest pageRequest) {
        List<TaskShowDTO> list = ITaskService.findBySendTaskStatus(pageRequest,
                RequestIdUtil.getMyId(request));
        PageInfo<TaskShowDTO> info = new PageInfo<>(list);
        return ResponseData.REP_GET_SUCCESS(info);
    }

    @ApiOperation(value = "查看任务申请列表接口", notes = "查看任务申请列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务id", required = true, dataType = "Long"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @GetMapping("applications/{taskId}")
    public Map<String, Object> findAllApplication(@ApiIgnore @PathVariable("taskId") Long taskId,
                                                  PageRequest pageRequest) {
        List<ProgrammerEntiretyDTO> list = ITaskService.findAllApplication(pageRequest, taskId);
        PageInfo<ProgrammerEntiretyDTO> info = new PageInfo<>(list);
        return ResponseData.REP_GET_SUCCESS(info);
    }

    @ApiOperation(value = "查询一个人全部参与的任务接口", notes = "查询一个人全部参与的任务")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @GetMapping("involvedInTask")
    public Map<String, Object> findByReceiveTaskUserId(HttpServletRequest request, PageRequest pageRequest) {
        List<TaskShowDTO> list = ITaskService.findByReceiveTaskUserId(pageRequest,
                RequestIdUtil.getMyId(request));
        PageInfo<TaskShowDTO> info = new PageInfo<>(list);
        return ResponseData.REP_GET_SUCCESS(info);
    }

    @ApiOperation(value = "查询接取任务的所有人接口", notes = "查询接取任务的所有人")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "list", value = "用户id数组", required = true, dataType = "List<Long>"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @GetMapping(value = "taskTasks", produces = "application/json")
    public Map<String, Object> findByList(@ApiIgnore @RequestParam("ids") List<Long> list) {
        return ResponseData.REP_GET_SUCCESS(ITaskService.findByList(list));
    }

    @ApiOperation(value = "查询共有多少人投递接口", notes = "查询共有多少人投递")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务id", required = true, dataType = "Long"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @GetMapping("totalNumber/{taskId}")
    public Map<String, Object> findCount(@PathVariable("taskId") Long taskId) {
        return ResponseData.REP_GET_SUCCESS(ITaskService.findCount(taskId));
    }

    //要改
    @ApiOperation(value = "投递简历接口", notes = "投递简历")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "userId", value = "投递人id", required = true, dataType = "Long"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 309, message = "申请重复"),
            @ApiResponse(code = 400, message = "查询失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @PostMapping(value = "delivery")
    public Map<String, Object> insertDelivery(@RequestBody DeliveryDTO deliveryDTO) {
        Integer count = ITaskService.insertDelivery(deliveryDTO.getTaskId(), deliveryDTO.getUserId());
        if (count == 1) {
            return ResponseData.REP_CREATED_SUCCESS();
        } else if (count == 2) {
            return ResponseData.REP_INTERNAL_SERVER_EXIST();
        }
        return ResponseData.REP_INVALID_ERROR();
    }

    @ApiOperation(value = "转一转接口", notes = "转一转")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务id", required = true, dataType = "Long")
    })
    @ApiResponses({
            @ApiResponse(code = 204, message = "操作成功"),
            @ApiResponse(code = 400, message = "操作失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @PutMapping("takeTurn/{taskId}")
    public Map<String, Object> takeTurn(@PathVariable("taskId") Long taskId, HttpServletRequest request) {
        Integer count = ITaskService.takeTurn(taskId, request);
        if (count == 1) {
            return ResponseData.REP_ACCEPTED_NO_HANDLE();
        }
        return ResponseData.REP_INVALID_ERROR();
    }

    @ApiOperation(value = "查询合同接口", notes = "查询合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "taskId", value = "任务id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "taskId", value = "任务id", required = true, dataType = "Long")
    })
    @ApiResponses({
            @ApiResponse(code = 204, message = "操作成功"),
            @ApiResponse(code = 400, message = "操作失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @GetMapping("contract/{taskId}/{receiveTaskId}/{sendTaskUserId}")
    public Map<String, Object> contract(@PathVariable("taskId") Long taskId,
                                        @PathVariable("receiveTaskId") Long receiveTaskId, @PathVariable(
            "sendTaskUserId") Long sendTaskUserId) {
        ContractDTO contract = ITaskService.contract(taskId, receiveTaskId, sendTaskUserId);
        return ResponseData.REP_GET_SUCCESS(contract);
    }

    @ApiOperation(value = "保存合同接口", notes = "保存合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "taskId", value = "任务id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "taskId", value = "任务id", required = true, dataType = "Long")
    })
    @ApiResponses({
            @ApiResponse(code = 204, message = "操作成功"),
            @ApiResponse(code = 400, message = "操作失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @PostMapping("contract")
    public Map<String, Object> saveContract(@RequestBody Contract contract) {
        Integer saveContract = ITaskService.saveContract(contract);
        if (saveContract == 1) {
            return ResponseData.REP_ACCEPTED_NO_HANDLE();
        }
        return ResponseData.REP_INVALID_ERROR();
    }

    @ApiOperation(value = "保存乙方签名接口", notes = "保存乙方签名")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "partyBImageUrl", value = "乙方签名地址", required = true, dataType = "Long"),
    })
    @ApiResponses({
            @ApiResponse(code = 204, message = "操作成功"),
            @ApiResponse(code = 400, message = "操作失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @PostMapping("contractImg")
    public Map<String, Object> savePartBImg(@RequestBody Contract contract) {
        Integer partBImg = ITaskService.updatePartBImg(contract);
        if (partBImg == 1) {
            return ResponseData.REP_ACCEPTED_NO_HANDLE();
        }
        return ResponseData.REP_INVALID_ERROR();
    }

    @ApiOperation(value = "查询工作地点接口", notes = "查询工作地点")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 204, message = "操作成功")
    })
    @GetMapping("getCitys")
    public Map<String, Object> getCitys() {
        List<String> citys = ITaskService.getCitys();
        return ResponseData.REP_GET_SUCCESS(citys);
    }

    @ApiOperation(value = "查询人才类别接口", notes = "查询人才类别")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功")
    })
    @GetMapping("getTalentCategory")
    public Map<String, Object> getTalentCategory() {
        List<String> talentCategory = ITaskService.getTalentCategory();
        return ResponseData.REP_GET_SUCCESS(talentCategory);
    }

}
